package cn.edu.lcvc.ebuy_23rj2.web.action.backstage.adminmanage;

import cn.edu.lcvc.ebuy_23rj2.model.Admin;
import cn.edu.lcvc.ebuy_23rj2.model.query.AdminQuery;
import cn.edu.lcvc.ebuy_23rj2.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backstage/adminmanage")
public class AdminManageController {
    @Autowired
    AdminService adminService;

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public String list(AdminQuery adminQuery, HttpServletRequest request) {
        request.setAttribute("list",adminService.getAdmins(adminQuery));
        return "/jsp/backstage/adminmanage/adminmanage.jsp";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/deleteAdmin")
    public String delete(Integer id, HttpServletRequest request, HttpSession session) {
        if (id != null) {
            Admin admin = (Admin) session.getAttribute("admin");
            //获取自己当前的登录账号信息
            if (admin != null) {//已经登录
                if (admin.getId() != id) {//如果删除的账号不是当前登录的账号
                    adminService.deleteAdmin(id);
                } else {//如果删除的是自己
                    request.setAttribute("myMessage", "删除失败：无法删除自己的账户！");
                }
            }
        }
        return "/backstage/adminmanage/list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/toAddAdmin")
    public String toAdd() {
        return "/jsp/backstage/adminmanage/adminadd.jsp";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/doAddAdmin")
    public String doAdd(Admin admin,HttpServletRequest request) {
        if(admin.getUsername()==null||admin.getUsername().length()==0){//账户名不能为空
            request.setAttribute("myMessage","添加失败：账户名不能为空");
        }else if(admin.getUsername()==null||admin.getUsername().length()==0){//性能不能为空
            request.setAttribute("myMessage","添加失败：姓名不能为空");
        }else if(adminService.countUsername(admin.getUsername())){//如果重名
            request.setAttribute("myMessage","添加失败：账户名重名，请选择其他的账户名！");
        }else{//如果不重名
            adminService.saveAdmin(admin);
        }
        return "/jsp/backstage/adminmanage/adminadd.jsp";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/toUpdateAdmin")
    public String toUppdate(Integer id,HttpServletRequest request){
        //根据id到数据库中查找对应的账户信息
        Admin admin=adminService.getAdminById(id);
        //将值传递到编辑页面中
        request.setAttribute("admin",admin);
        return "/jsp/backstage/adminmanage/adminupdate.jsp";
    }

    @RequestMapping(method = RequestMethod.POST, value = "/doUpdateAdmin")
    public String doUppdate(Admin admin,HttpServletRequest request){
        if(admin.getUsername()==null||admin.getUsername().length()==0){//账户名不能为空
            request.setAttribute("myMessage","编辑失败：账户名不能为空");
        }else if(admin.getName()==null||admin.getName().length()==0){//姓名不能为空
            request.setAttribute("myMessage","编辑失败：姓名不能为空");
        }else if(adminService.countByUsernameAndNotId(admin.getUsername(),admin.getId())){//如果重名
            request.setAttribute("myMessage","编辑失败：账户名重名，请选择其他的账户名！");
        }else{//如果不重名
            adminService.updateAdmin(admin);
        }
        //3.跳转回编辑页面
        //根据id到数据库中查找对应的账户信息
        admin=adminService.getAdminById(admin.getId());
        //将值传递到编辑页面中
        request.setAttribute("admin",admin);
        return "/jsp/backstage/adminmanage/adminupdate.jsp";
    }

}
